//*************************************************************************************************
//************************ (C) 2001 - 2006, Eleven Engineering Incorporated ***********************
//*************************************************************************************************
//**
//**		Tabs:	This file looks best with tab stops set every 6 spaces.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**          File:  Delay.asm
//**       Project:  XInc2 Library
//**       Created:  04 Jan 2003 by Ryan Northcott
//**       Revised:  08 Sep 2006 by Dan Beutel
//**
//**   Description:  Routines to delay a certain number of clock ticks.
//**
//** Compatibility:  Requires XInA v4.4 or higher.
//**
//**    Disclaimer:  You may incorporate this sample source code into your program(s) without
//**                 restriction.  This sample source code has been provided "AS IS" and the
//**                 responsibility for its operation is yours.  You are not permitted to
//**                 redistribute this sample source code as "Eleven sample source code" after
//**                 having made changes.  If you are going to re-distribute the source, we require
//**                 that you make it clear in the source that the code was descended from Eleven
//**                 sample source code, but that you have made changes.
//**
//*************************************************************************************************
//*************************************************************************************************
//**
//**	Delay
//**	DelayLong
//**	DelayReallyLong
//**
//*************************************************************************************************
//*************************************************************************************************

#ifndef __DELAY_UTILS__
#define __DELAY_UTILS__

//=============================================================================
// Input Params:	r1 = Delay Length
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Delays for a given amount of time.  This function will return
//			after (5 + 2*r1) instruction times have elapsed.
//=============================================================================
Delay:
					st	r1, sp, 0

					add	r1, r1, 0
					bc	ZS, Delay_END
	Delay_loop:
					sub	r1, r1, 1
					bc	ZC, Delay_loop
Delay_END:
					ld	r1, sp, 0

					jsr	r6, r6



//=============================================================================
// Input Params:	None
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Delays for a long time (returns after 131074 instruction
//			times have elapsed which is roughly 1/12 of a second when
//			using a 12MHz clock).
//=============================================================================
DelayLong:
					st	r0, sp, 0

					mov	r0, 0xFFFF
	DelayLong_Loop:
					sub	r0, r0, 1
					bc	ZC, DelayLong_Loop
DelayLong_END:
					ld	r0, sp, 0

					jsr	r6, r6



//=============================================================================
// Input Params:	None
// Output Params:	None
//-----------------------------------------------------------------------------
// Description:	Delays for a really long time (returns after 524299
//			instruction times have elapsed which is roughly 1/3 of a
//			second when using a 12MHz clock).
//=============================================================================
DelayReallyLong:
					st	r6, sp, 0
					add	sp, sp, 1

					jsr	r6, DelayLong
					jsr	r6, DelayLong
					jsr	r6, DelayLong
					jsr	r6, DelayLong
DelayReallyLong_END:
					sub	sp, sp, 1
					ld	r6, sp, 0

					jsr	r6, r6
					

#endif